package org.lwd.datalink.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import lombok.extern.slf4j.Slf4j;
import org.lwd.datalink.zookeeper.DLinkZkPathDef;
import org.lwd.datalink.zookeeper.DLinkZkUtils;
import org.lwd.datalink.zookeeper.ManagerMetaData;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * rest server
 *
 * @Author: weidong.lin
 * @Date: create in 2024/4/9 14:55
 */
@Slf4j
public class RestServerUtils {

    private static final RestTemplate restTemplate = new RestTemplate();

    private static final LoadingCache<String, ManagerMetaData> cache = CacheBuilder
            .newBuilder()
            .expireAfterWrite(10L, TimeUnit.SECONDS)
            .build(new CacheLoader<String, ManagerMetaData>() {
                @Override
                public ManagerMetaData load(String key) throws Exception {
                    byte[] bytes = DLinkZkUtils.get().zkClient().readData(DLinkZkPathDef.ManagerActiveNode, true);
                    if (bytes != null) {
                        return JSON.parseObject(bytes, ManagerMetaData.class);
                    }
                    return null;
                }
            });

    public static void executeRemote(Object obj, String url) throws Exception {
        log.error("data: {}, url: {}", JSON.toJSONString(obj), url);
        // TODO 暂时不跟Manager有交集，后续在搞
//        ManagerMetaData managerMetaData = cache.getUnchecked(DLinkZkPathDef.ManagerActiveNode);
//        if (managerMetaData != null) {
//            String prefix = "http://" + managerMetaData.getAddress() + ":" + managerMetaData.getHttpPort();
//            HttpHeaders headers = new HttpHeaders();
//            headers.setContentType(MediaType.APPLICATION_JSON);
//            HttpEntity request = new HttpEntity(obj != null ? JSONObject.toJSONString(obj, SerializerFeature.WriteClassName) : null, headers);
//            restTemplate.postForObject(prefix + url, request, Map.class);
//        }
    }
}

